package com.kaishengit.service;

import java.util.List;

import com.kaishengit.dao.ProjectDao;
import com.kaishengit.dao.UserProjectDao;
import com.kaishengit.entity.Project;
import com.kaishengit.entity.UserProject;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKUtil;

public class ProjectService {
	
	private ProjectDao projectDao=new ProjectDao();
	private UserProjectDao userProjectDao=new UserProjectDao();
	
	public List<Project> getUserProject(String id){
		return projectDao.findByUserId(id);
  }
	
	public UserProject findById(String userId,String projectId){
		return userProjectDao.findByProjectId(projectId,userId);
	}
	
	public void save(String name,String desc,String manager,String worker){
		Project p=new Project();
		p.setId(PKUtil.getId());
		p.setDesc(desc);
		p.setName(name);
		p.setCreatetime(DateUtil.getNowDateTime());
		projectDao.save(p);
		
		UserProject up=new UserProject();
		up.setUserId(manager);
		up.setProjectId(p.getId());
		up.setRole("manager");
		
		userProjectDao.save(up);
		
		String[] workerIds=worker.split(",");
		for(String id:workerIds){
			UserProject userProject=new UserProject();
			userProject.setUserId(id);
			userProject.setProjectId(p.getId());
			userProject.setRole("worker");
			userProjectDao.save(userProject);
			
			
		}
		
	}
}